package juc.juccore.threadlocal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 加锁解决SimpleDataFormat线程不安全问题
 */
public class ThreadLocalNormalUsage04 {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            int a = i;
            executorService.submit(new Thread(() -> {
                String data = new ThreadLocalNormalUsage01().data(a * 10);
                System.out.println(data);
            }));
        }
        executorService.shutdown();
    }

    public String data(int seconds) {
        Date date = new Date(1000 * seconds);
        String s;
        synchronized (ThreadLocalNormalUsage05.class) {
            s = format.format(date);
        }
        return s;
    }
}
